package cn.element.netty.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

import static io.netty.buffer.ByteBufUtil.appendPrettyHexDump;
import static io.netty.util.internal.StringUtil.NEWLINE;

public class TestByteBuf {

    public static void main(String[] args) {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        log(buf);
        
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 300; i++) {
            sb.append("a");
        }
         
        buf.writeBytes(sb.toString().getBytes());
        log(buf);
    }
    
    public static void log(ByteBuf buf) {
        int len = buf.readableBytes();
        int row = len / 16 + (len % 15 == 0 ? 0 : 1) + 4;
        StringBuilder sb = new StringBuilder(row *80 * 2);
        sb.append("read index: ").append(buf.readerIndex())
          .append(" write index: ").append(buf.writerIndex())
          .append(" capacity: ").append(buf.capacity())
          .append(NEWLINE);
        appendPrettyHexDump(sb, buf);
        System.out.println(sb.toString());
    }

}
